** Collapse similarity measures at different levels of geography
** JHL  

** Similarity measures 
** For each pair of stores i and j in chain c, average across products and quarters 
** 1. Weekly correlation in deviation from quarterly mean

*************************************
** Set up workspace
*************************************
version 15.0
clear all
set more off
** include "/scratch/midway2/jleung/replication/do/set_path.do"
cd "${path_home}"
adopath + ../programs

** log using "${path_log}/05_similarity_top1", text replace

set matsize 11000, perm 
set maxvar 32767, perm 

*************************************
** Start work here
*************************************
timer on 1 

*************************************
** [1] Collapse similarity measures
*************************************

** --array=1040,1290,1303,1362,1463,1484,1493,3603,7080,7260,7734,8404
** Parallelize for speed gains
local m: environment SLURM_ARRAY_TASK_ID 

** [1.1] Collapse similarity measures by parent (same parent or different parent) 
** foreach m in 1040 1290 1303 1362 1463 1484 1493 3603 7080 7260 7734 8404 { 
	foreach cc in D F M {
		** Skip if file does not exit due to insufficient observations for construction
		cap confirm file "${path_dta}/nielsen/`m'_top1_`cc'_cor"
		if _rc!=0 {
			break 
		}
		else {
			use "${path_dta}/nielsen/`m'_top1_`cc'_cor", clear 

			collapse (mean) correlation count (count) store_code_uc_1, by(parent_code_1 same_parent) 
			sort parent_code_1 same_parent
			
			* 1 = 0, 2 = 1
			** egen group = group(same_parent)
			gen group = 1 if same_parent==0 
				replace group = 2 if same_parent==1 			
			drop same_parent 
			reshape wide correlation count store_code_uc_1, i(parent_code_1) j(group)
			
			** gen differences between correlations for same parent, same vs. different state as measure of rigidity
			gen d_cor = correlation2 - correlation1
			
			save "${path_dta}/nielsen/`m'_top1_`cc'_cor_parent", replace 
		}
	}
** }

** [1.1] Collapse similarity measures by state x parent (same state-parent or different state-parent) 
** foreach m in 1040 1290 1303 1362 1463 1484 1493 3603 7080 7260 7734 8404 { 
	foreach cc in D F M {
		** Skip due to insufficient obsevations for construction
		cap confirm file "${path_dta}/nielsen/`m'_top1_`cc'_cor"
		if _rc!=0 {
			break 
		}
		else {
			use "${path_dta}/nielsen/`m'_top1_`cc'_cor", clear 
			gen same_state = fips_state_code_1 == fips_state_code_2 

			collapse (mean) correlation count (count) store_code_uc_1, by(parent_code_1 same_state same_parent) 
			sort parent_code_1 same_parent same_state
			
			* 1 = 0 0, 2 = 0 1, 3 = 1 0, 4 = 1 1
			** egen group = group(same_parent same_state)
			gen group = 1 if same_parent==0&same_state==0
				replace group = 2 if same_parent==0&same_state==1 
				replace group = 3 if same_parent==1&same_state==0
				replace group = 4 if same_parent==1&same_state==1
			drop same_parent same_state 
			reshape wide correlation count store_code_uc_1, i(parent_code_1) j(group)
			
			** gen differences between correlations for same parent, same vs. different state as measure of rigidity
			cap noi gen d_cor_wi_parent = correlation4 - correlation3
			cap noi gen d_cor_wi_state = correlation4 - correlation2 

			
			save "${path_dta}/nielsen/`m'_top1_`cc'_cor_parent_x_state_by_parent", replace 
		}
	}
** }

*************************************
** Close workspace
*************************************
timer off 1
timer list 1
** log close
